﻿using Microsoft.VisualStudio.Shell;
using System;
using System.ComponentModel.Design;
using Task = System.Threading.Tasks.Task;

namespace JoinBoxAlignPlug
{
    internal sealed class CmdDisableSave
    {
        public const int CommandId = 4177;

        public static readonly Guid CommandSet = new Guid("32b0c337-ecfc-4210-ab91-1db5af28869d");

        private readonly AsyncPackage package;

        private CmdDisableSave(AsyncPackage package, OleMenuCommandService commandService)
        {
            this.package = package ?? throw new ArgumentNullException(nameof(package));
            commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));

            var menuCommandID = new CommandID(CommandSet, CommandId);
            var menuItem = new MenuCommand(this.Execute, menuCommandID);
            commandService.AddCommand(menuItem);
        }

        public static CmdDisableSave Instance
        {
            get;
            private set;
        }

        private Microsoft.VisualStudio.Shell.IAsyncServiceProvider ServiceProvider
        {
            get => this.package;
        }

        public static async Task InitializeAsync(AsyncPackage package)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);

            OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
            Instance = new CmdDisableSave(package, commandService);
        }


        private void Execute(object sender, EventArgs e)
        {
            // 取反这个开关
            EditorViewTextViewCreationListener.DisableSave = !EditorViewTextViewCreationListener.DisableSave;
        }
    }
}
